package liuytset.com;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class SetGetHeader{
	/**
	 * 通过get方法访问链接地址，并设置相应的header信息。
	 *  
	 *   参看：
	 *   http://www.ibm.com/developerworks/cn/opensource/os-httpclient/
	 *   
	 *   http://playfish.javaeye.com/blog/171221 评论部分
	 * 
	 * */
	public static void main(String[] args) {
		  
		  //构造HttpClient的实例
		  HttpClient httpClient = new HttpClient();
		  //创建GET方法的实例
		  GetMethod getMethod = new GetMethod("http://202.99.122.17:3000");		  
		  List<Header> headers = new ArrayList<Header>();       
 
		  headers.add(  new Header( "Accept", "image/gif, image/x-xbitmap, image/jpeg, " +  
				           "image/pjpeg, application/xaml+xml, application/vnd.ms-xpsdocument, " +  
				           "application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, " +  
				           "application/vnd.ms-powerpoint, application/msword, application/x-silverlight, " +  
				           "application/QVOD, */*" ));  
		  headers.add( new Header( "Accept-Language", "zh-cn" ) );  
		  headers.add( new Header( "Accept-Encoding", "gzip, deflate" ) );  
		  headers.add( new Header( "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; " +  
				   "Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)" ) );  
		  //headers.add(new Header("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)")); 
		  headers.add( new Header( "Connection", "Keep-Alive" ) );  
		  	  
		  /**
		   *  测试参数：
		   *  
		   *  HEAD / HTTP/1.1
		   *  Host: 202.99.122.17
		   *  cnc-uniauth-serial: 1234567890
		   *  cnc-uniauth-user: 02227219140
		   *  cnc-uniauth-spid: 002
		   *  cnc-uniauth-bizid: 002002
		   *  Connection: close
		   *  Content-Length: 0
		   *  
		   *  
		   *  --------------------
		   *   HEAD / HTTP/1.1
		   *   Host: 202.99.122.17
		   *   cnc-uniauth-serial: 1234567890
		   *   cnc-uniauth-ip: 111.162.138.231
		   *   cnc-uniauth-spid: 002
		   *   cnc-uniauth-bizid: 002002
		   *   Connection: close
		   *   Content-Length: 0
		   *   
		   * */
		  
	      //String  crlr = "\r\n"; //回车换行符 ，加上反而不好使
 	      String  crlr = "";
	      headers.add(new Header("Host", "202.99.122.17"+crlr));   
	      headers.add(new Header("cnc-uniauth-serial", "1234567890"+crlr));   
	      headers.add(new Header("cnc-uniauth-ip", "111.162.138.231"+crlr));   
	      //headers.add(new Header("cnc-uniauth-ip", ""+crlr));
	      //headers.add(new Header("cnc-uniauth-user", "27219140"+crlr));   
	      //headers.add(new Header("cnc-uniauth-user", ""+crlr));
	      headers.add(new Header("cnc-uniauth-spid", "002"+crlr));   
	      headers.add(new Header("cnc-uniauth-bizid", "002002"+crlr));  
		  
	      for ( Iterator iter = headers.iterator(); iter.hasNext(); ) {  
	    	  Header header = (Header) iter.next();  
	    	  getMethod.setRequestHeader(header);  
	      }  
	      
	      
		  
		  //使用系统提供的默认的恢复策略
		  getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());
		  try {
			   //执行getMethod
			   int statusCode = httpClient.executeMethod(getMethod);
			   if (statusCode != HttpStatus.SC_OK) {
				   System.err.println("Method failed: "+ getMethod.getStatusLine());
			   }else{
				   System.out.println("ok!!");
			   }
			   //读取内容 
			   byte[] responseBody = getMethod.getResponseBody();
			   //处理内容
			   System.out.println(new String(responseBody));
		  } catch (HttpException e) {
				   //发生致命的异常，可能是协议不对或者返回的内容有问题
				   System.out.println("Please check your provided http address!");
				   e.printStackTrace();
		  } catch (IOException e) {
				   //发生网络异常
				   e.printStackTrace();
		  } finally {
				   //释放连接
				   getMethod.releaseConnection();
		  }
	}
}
